/*
 * @Author: Dorange.Wang
 * @Date: 2021-07-07 13:51:33
 * @LastEditors: your name
 * @LastEditTime: 2021-07-20 14:08:33
 * @Description: file content
 */
import storage from "store2";
import router from "@/router";
import store from "@/store";
import { ACCESS_TOKEN } from "@/utils/const";

const allowList = ["/user/login"];
// const loginPath = '/user/login'

router.beforeEach(async (to, from, next) => {
  to.meta && (typeof to.meta.title !== 'undefined' && (document.title = to.meta.title))
  if (storage.get(ACCESS_TOKEN)) {
    console.log(to, from, next);
    console.log("store", store);
    if (store.getters.userInfo.id) {
      next();
    } else {
      const infoPromise = store.dispatch("loadUserInfo");
      const menusPromise = store.dispatch("loadUserMenus");
      await Promise.all([infoPromise, menusPromise]);
      next();
    }
  } else if (allowList.includes(to.path)) {
    next();
  } else {
    store.dispatch("userLogout");
  }
});
